package com.wzt.lianfirecontrol.utils;

import android.os.Bundle;
import android.os.Handler;

import com.wzt.lianfirecontrol.activity.BaseActivity;
import com.wzt.lianfirecontrol.bean.ConstData;
import com.wzt.lianfirecontrol.http.HttpHelper;
import com.wzt.lianfirecontrol.http.json.ParseJsonData;
import com.wzt.lianfirecontrol.http.Url;

import org.json.JSONObject;

import java.io.File;
import java.util.HashMap;

public class MinioUtils {

    /**
     * minio
     */
//    //public static final String MINIO_IP = "http://121.196.189.225:9000";
//    public static final String MINIO_IP = "http://220.178.27.116:7000";
//    public static final String MINIO_ACCESSKEY = "minioadmin";
//    public static final String MINIO_SECRETKEY = "minioadmin";
////    public static final String MINIO_ACCESSKEY = "minio";
////    public static final String MINIO_SECRETKEY = "KDLApwd2020";
//    public static final String MINIO_BUCKET_NAME = "myminio";
//    public static final String PACKAGE_NAME = "app";
//    public static final String MINIO_IMG_PATH = MINIO_IP + "/" + MINIO_BUCKET_NAME + "/" + PACKAGE_NAME + "/";

//    public static void upLoadFile(final Context context, int imgWhat, File file, String img, Handler handler, int successWhat, int failWhat){
//        AsyncExecutor.execute(new Runnable() {
//            @Override
//            public void run() {
//                Message message = new Message();
//                Bundle bundle = new Bundle();
//                bundle.putInt("imgWhat", imgWhat);
//                // 上传图片到minio服务器
//                try {
//                    MinioClient minioClient = new MinioClient(MINIO_IP, MINIO_ACCESSKEY, MINIO_SECRETKEY);
//                    boolean isExist = minioClient.bucketExists(MINIO_BUCKET_NAME);
//                    if (!isExist) {
//                        minioClient.makeBucket(MINIO_BUCKET_NAME);
//                    }
//
//                    Date date = new Date();
//                    String fileName = imgWhat + UserInfoModel.getUserAccount(context) + date.getTime() + ".jpg";
//                    minioClient.putObject(MINIO_BUCKET_NAME, "app/"+fileName, file.getPath(), null);
//                    bundle.putString("fileName", fileName);
//                    bundle.putString("img", img);
//                    message.setData(bundle);
//                    message.what = successWhat;
//                    handler.sendMessage(message);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                    message.what = failWhat;
//                    message.setData(bundle);
//                    handler.sendMessage(message);
//                }
//            }
//        });
//    }
//
//    /**
//     * 删除服务器上文件
//     * @param fileName
//     */
//    public static void removeFile(String fileName){
//        if (StringUtils.isEmpty(fileName)){
//            return;
//        }
//        AsyncExecutor.execute(new Runnable() {
//            @Override
//            public void run() {
//                // 删除图片到minio服务器
//                try {
//                    MinioClient minioClient = new MinioClient(MINIO_IP, MINIO_ACCESSKEY, MINIO_SECRETKEY);
//                    minioClient.removeObject(MINIO_BUCKET_NAME, fileName);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        });
//    }

    public static void upLoadFile(final BaseActivity activity, int imgWhat, File file, Handler handler){
        if (!Utils.hasNetwork(activity)){
            ToastUtils.showToast(activity, "网络未连接");
            return;
        }
        HttpHelper homeListHttpHelper = new UpdateImgListHttpHelper(activity, Url.UP_IMG_URL, imgWhat + ConstData.COMMIT_IMG_WHAT, handler, file);
        homeListHttpHelper.setParseJsonData(new UpdateImgParseJsonData());
        homeListHttpHelper.getHttpFileRequest();
    }

    /**
     * 删除服务器上文件
     * @param link
     */
    public static void removeFile(BaseActivity activity, String link, Handler handler){
        if (StringUtils.isEmpty(link)){
            return;
        }
        HashMap<String, String> url_params = new HashMap<>();
        url_params.put("link", link);
        String url = Url.getGetUrl(activity, Url.DELETE_IMG_URL, url_params);
        HttpHelper homeListHttpHelper = new UpdateImgListHttpHelper(activity, url, ConstData.DELE_IMG_WHAT, handler);
        homeListHttpHelper.setParseJsonData(new UpdateImgParseJsonData());
        homeListHttpHelper.getHttpRequest();
    }


    private static class UpdateImgListHttpHelper extends HttpHelper {

        public UpdateImgListHttpHelper(BaseActivity activity, String url, int what, Handler handler) {
            super(activity, url, what, handler);
        }

        public UpdateImgListHttpHelper(BaseActivity activity, String url, int what, Handler handler, File file) {
            super(activity, url, what, handler, file);
        }
    }

    protected static class UpdateImgParseJsonData extends ParseJsonData {

        public UpdateImgParseJsonData() {
        }

        @Override
        public void analyzeResult(String dataStr, Bundle bundle, int what) {
            try {
                if (what >= ConstData.COMMIT_IMG_WHAT) {
                    JSONObject jsonObject = new JSONObject(dataStr);
                    String link = jsonObject.getString("link");
                    bundle.putString("fileName", link);
                }
                bundle.putBoolean(REQUEST_OK, true);
            }catch (Exception e){
                bundle.putBoolean(REQUEST_OK, false);
                bundle.putString(ERROR_MSG, ANALYZE_ERROR_MSG);
            }
        }
    }

}
